﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using WebCommit.Common.Entitys;
using System.Threading;

namespace WebCommit.Crawler
{
	public abstract class OldWorker
	{
		private BackgroundWorker BackWorker = new BackgroundWorker();
		protected List<WebResourceEntity> WebList;
		protected HashSet<string> UrlHistory;
		private List<WebResourceEntity> ResolveList = new List<WebResourceEntity>();
		public OldWorker(List<WebResourceEntity> webList, HashSet<string> urlHistory)
		{
			BackWorker.WorkerSupportsCancellation = true;
			BackWorker.DoWork += new DoWorkEventHandler(Worker_DoWork);
			BackWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
			WebList = webList;
			UrlHistory = urlHistory;
		}

		public delegate void OnStarted(object sender, WorkerEventArgs e);
		public delegate void OnProgressChanged(object sender, WorkerEventArgs e);
		//public delegate void OnPaused(object sender, CrawlWorkerEventArgs e);
		public delegate void OnStoped(object sender, WorkerEventArgs e);

		public event OnStarted Started;
		public event OnProgressChanged ProgressChanged;
		//public event OnPaused Paused;
		public event OnStoped Stoped;

		public WorkerStatus CurrentStatus = WorkerStatus.Stoped;

		void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			if (ProgressChanged != null)
				ProgressChanged.DynamicInvoke(this, new WorkerEventArgs() { UrlList = e.Result as List<WebResourceEntity> });

			if (CurrentStatus != WorkerStatus.Stoped)
			{
				Thread.Sleep(10);
				Start();
			}
		}


		void Worker_DoWork(object sender, DoWorkEventArgs e)
		{
			List<WebResourceEntity> webList = PreWork(WebList);
			if (CurrentStatus != WorkerStatus.Working)
			{
				this.CurrentStatus = WorkerStatus.Working;
				if (Started != null)
				{
					Started.DynamicInvoke(this, new WorkerEventArgs() { UrlList = webList });
				}
			}
			List<WebResourceEntity> list = DoWork(webList);
			e.Result = list;
		//	WebList.AddRange(list);
			ResolveList.AddRange(webList);
			
		}

		/// <summary>
		/// 预处理，返回需要处理的WebResource
		/// </summary>
		/// <param name="webList"></param>
		/// <returns></returns>
		public abstract List<WebResourceEntity> PreWork(List<WebResourceEntity> webList);

		/// <summary>
		/// 处理，传入预处理返回的List
		/// </summary>
		/// <param name="webList"></param>
		/// <returns></returns>
		public abstract List<WebResourceEntity> DoWork(List<WebResourceEntity> webList);
		

		public void Start()
		{
			//this.CurrentStatus = WorkerStatus.Working;
			BackWorker.RunWorkerAsync();
		}

		//public void Pause()
		//{
		//    this.CurrentStatus = CrawlWorkerStatus.Paused;
		//    BackWorker.CancelAsync();
		//}

		public void Stop()
		{
			BackWorker.CancelAsync();
			this.CurrentStatus = WorkerStatus.Stoped;
			if (Stoped != null)
			{
				Stoped.DynamicInvoke(this, new WorkerEventArgs() { UrlList = ResolveList });
			}

		}
	}


	public class WorkerEventArgs : EventArgs
	{
		/// <summary>
		/// 准备处理的列表或刚处理的列表或所有已处理的列表
		/// </summary>
		public List<WebResourceEntity> UrlList
		{
			get;
			set;
		}
	}

	public enum WorkerStatus
	{
		Stoped,
		Working,
		//Paused
	}
}
